import { _decorator, Component, EventTouch, Node } from 'cc';
import { SpriteSwitch } from '../AshFramework/UI/SpriteSwitch';
import { BUYTYPE, ITEMTYPE, NUMBER } from './GameConstant';
import { ZTool } from '../AshFramework/Utils/ZTool';
import { Item } from './Item';
import { PrefsManager } from './PrefsManager';
import Banner from '../Banner';
import { ShowWeapon } from './ShowWeapon';
import { ShowPanel } from './ShowPanel';
import { AudioManager, Audios } from '../AshFramework/Managers/AudioManager';
const { ccclass, property } = _decorator;

@ccclass('Shop')
export class Shop extends Component {

    @property(Node)
    PFView: Node = null;

    @property(Node)
    GSView: Node = null;

    @property(Node)
    PFBtn: Node = null;

    @property(Node)
    GCBtn: Node = null;

    @property(Node)
    GSBtn: Node = null;

    @property(Node)
    UnluckButtonNode: Node = null;


    TargetBtn: Node = null;
    TargetView: Node = null;
    TargetItem: Node = null;//选中
    TargetClick: Node = null;//点击
    ItemType: ITEMTYPE = ITEMTYPE.PF;

    protected onLoad(): void {
        this.TargetBtn = this.PFBtn;
        this.TargetBtn.getComponent(SpriteSwitch).isPitch = true;
        this.TargetView = this.PFView;
    }

    switchBtn(event: EventTouch, type: string) {
        if (event.target === this.TargetBtn) return;
        AudioManager.PlaySound(Audios.Click);
        if (this.TargetBtn && this.TargetBtn.getComponent(SpriteSwitch).isPitch) {
            this.TargetBtn.getComponent(SpriteSwitch).switchStyle();
        }
        if (this.TargetView && this.TargetView.active) {
            this.TargetView.active = false;
        }

        this.TargetBtn = event.target;
        this.TargetBtn.getComponent(SpriteSwitch).switchStyle();
        switch (type) {
            case "1":
                this.TargetView = this.PFView;
                this.ItemType = ITEMTYPE.PF;
                break;
            case "2":
                // this.TargetView = this.GCView;
                this.ItemType = ITEMTYPE.GC;
                break;
            case "3":
                this.TargetView = this.GSView;
                this.ItemType = ITEMTYPE.GS;
                break;
        }
        this.TargetView.active = true;
        this.hideButton();
        this.showWindow();
        this.initItem();
    }

    showWindow(itemNumber: NUMBER = PrefsManager.Instance.userData.checkedItem[ZTool.GetEnumKeyByValue(ITEMTYPE, this.ItemType)]) {
        ShowPanel.Instance.show(this.ItemType, itemNumber);
    }

    initItem() {
        let items = this.TargetView.getChildByName("view").getChildByName("content").children;
        items.forEach(item => {
            let ts = item.getComponent(Item);

            //修改拥有的Item的样式
            if (PrefsManager.Instance.userData.haveItem[ZTool.GetEnumKeyByValue(ITEMTYPE, this.ItemType)].findIndex(e => e == ts.ItemNumber) != -1) {
                ts.isHave(true);
            } else {
                ts.isHave(false);
            }

            //选出目前选中的Item
            if (ts.ItemNumber == PrefsManager.Instance.userData.checkedItem[ZTool.GetEnumKeyByValue(ITEMTYPE, this.ItemType)]) {
                ts.checked(true);
                ts.click(true);
                // console.error(ZTool.GetEnumKeyByValue(ITEMTYPE, this.ItemType), ZTool.GetEnumKeyByValue(NUMBER, ts.ItemNumber));
                this.TargetItem = item;
                this.TargetClick = item;
                if (ITEMTYPE.PF == this.ItemType) {
                    this.scheduleOnce(() => { ShowWeapon.Instance.changeSkin(ts.ItemNumber); }, 0.1)
                }
            } else {
                ts.checked(false);
                ts.click(false);
            }

            if (this.ItemType == ITEMTYPE.GS) {
                if (PrefsManager.Instance.userData.unluckGS.find(e => e == ts.ItemNumber)) {
                    ts.isUnluck(true);
                } else {
                    ts.isUnluck(false);
                }
            }
        })
    }

    itemBtn(event: EventTouch) {
        // if (event.target === this.TargetItem) return;
        const item: Item = event.target.getComponent(Item);
        this.hideButton();
        if (this.TargetClick) this.TargetClick.getComponent(Item).click(false);
        this.TargetClick = event.target;
        this.TargetClick.getComponent(Item).click(true);

        if (this.ItemType == ITEMTYPE.PF) {
            ShowWeapon.Instance.changeSkin(item.ItemNumber);
            if (item.IsHave) {
                PrefsManager.Instance.userData.checkedItem.PF = item.ItemNumber;
            }
        } else if (this.ItemType == ITEMTYPE.GC) {
        } else if (this.ItemType == ITEMTYPE.GS) {
            this.showWindow(item.ItemNumber);
            if (item.IsHave) {
                PrefsManager.Instance.userData.checkedItem.GS = item.ItemNumber;
            }
        }

        console.log(ZTool.GetEnumKeyByValue(ITEMTYPE, item.ItemType));

        if (item.ItemType == ITEMTYPE.GS && !item.IsUnluck) {
            //点击未解锁的挂饰
            // console.error(`未解锁！`);
            AudioManager.PlaySound(Audios.Forbid);
        } else {
            AudioManager.PlaySound(Audios.Click);
        }

        if (item.IsHave) {
            if (this.TargetItem) this.TargetItem.getComponent(Item).checked(false);
            this.TargetItem = event.target;
            this.TargetItem.getComponent(Item).checked(true);
        } else {
            //玩家点击没有拥有得商品 -- 显示购买或观看视频的按钮
            this.showButton();
        }
    }

    showButton() {
        if (!this.UnluckButtonNode) return;
        this.UnluckButtonNode.active = true;
        this.UnluckButtonNode.getComponent(SpriteSwitch).switchUnluckStyle(this.TargetClick.getComponent(Item).BuyType);
    }

    hideButton() {
        if (this.UnluckButtonNode) this.UnluckButtonNode.active = false;
    }

    clickBtn() {
        const item: Item = this.TargetClick.getComponent(Item);
        const ItemType: string = ZTool.GetEnumKeyByValue(ITEMTYPE, item.ItemType);
        if (BUYTYPE.GOLD == item.BuyType) {
            //购买金币解锁
            if (PrefsManager.Instance.userData.Gold >= item.Price) {
                AudioManager.PlaySound(Audios.OpenTreasureBox);
                //金币足够--购买
                PrefsManager.Instance.userData.Gold -= item.Price;
                PrefsManager.Instance.userData.haveItem[ItemType].push(item.ItemNumber);
                PrefsManager.Instance.userData.checkedItem[ItemType] = item.ItemNumber;
                PrefsManager.Instance.saveData();
                item.buy();
                this.initItem();
                this.hideButton();
            } else {
                //金币不足够
                AudioManager.PlaySound(Audios.Forbid);
            }
        } else if (BUYTYPE.VIDEO == item.BuyType) {
            //观看视频解锁
            // PrefsManager.Instance.userData.haveItem[ItemType].push(item.ItemNumber);
            // PrefsManager.Instance.userData.checkedItem[ItemType] = item.ItemNumber;
            // PrefsManager.Instance.saveData();
            // item.buy();
            // this.initItem();
            // this.hideButton();
            AudioManager.PlaySound(Audios.Click);
            Banner.Instance.ShowVideoAd(() => {
                AudioManager.PlaySound(Audios.OpenTreasureBox);
                PrefsManager.Instance.userData.haveItem[ItemType].push(item.ItemNumber);
                PrefsManager.Instance.userData.checkedItem[ItemType] = item.ItemNumber;
                PrefsManager.Instance.saveData();
                item.buy();
                this.initItem();
                this.hideButton();
            })
        }
    }

    protected onEnable(): void {
        this.showWindow();
        this.initItem();
    }

}


